#pragma once
#include <iostream>
#include "Tcp_Server.hpp"
#include "Protocol.hpp"
#include <pthread.h>
#include "Log.hpp"
#define PORT 8081

class HttpServer
{
private:
    int _port;
    TcpServer *_tcp_server;
    bool _stop;

public:
    HttpServer(int port = PORT) : _port(port),_tcp_server(nullptr),_stop(false)
    {}

    void InitServer()
    {
        _tcp_server=TcpServer::getinstance(_port);
    }

    void Loop()
    {   
        LOG(INFO,"Loop begin");
        int listensock=_tcp_server->GetListenSock();
        while(!_stop)
        {
            sockaddr_in peer;
            socklen_t len=sizeof(peer);
            int sock=accept(listensock,(struct sockaddr*)&peer,&len);
            if(sock<0) continue;
            
            LOG(INFO,"get a new link");
            int* _sock=new int(sock);
            pthread_t tid;
            pthread_create(&tid,nullptr,Entrance::HandlerRequest,_sock);
            pthread_detach(tid);
        }
    }

    ~HttpServer()
    {   
    }
};